package com.xbongbong.saas.factory.publicrule;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.saas.domain.entity.CustomerRuleEntity;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.List;

/**
 * 无新建机会客户规则校验、数据获取类
 *
 * @author : chy
 * @version v3.22
 * @date 创建时间：2018-5-16 下午4:24:22
 */
public class OperateNoOpportunityRuleValue implements IOperateRuleValue {

    private static final Logger LOG = LoggerFactory.getLogger(OperateNoOpportunityRuleValue.class);

    @Override
    public void verify(String ruleValue) throws XbbException {
        OperateCheckRuleValue.checkVerifyMsg(ruleValue);
    }

    @Override
    public Object toObject(CustomerRuleEntity rule) {
        if (rule == null) {
            return null;
        }
        Object retObject;

        try {
            retObject = JSON.parse(rule.getRuleValue());
        } catch (Exception e) {
            retObject = null;
            LOG.error("corpid:" + rule.getCorpid() + " ruleId:" + rule.getId() + "------json格式错误\n " + rule.getRuleValue());
        }

        return retObject;
    }

    @Override
    public List<String> getAllSpecialUser(Object ruleObject) {
        return OperateCheckRuleValue.checkSpecialUser(ruleObject);
    }

    /**
     * 获取规则配置中的标签Id
     *
     * @param ruleObject
     * @return
     */
    @Override
    public List<Long> getCustomerLabelIds(Object ruleObject) {
        return OperateCheckRuleValue.checkClueLabelIds(ruleObject);
    }

    @Override
    public Integer getStatus(CustomerRuleEntity customerRule) {
        if (customerRule != null) {
            return customerRule.getStatus();
        }
        return null;
    }

    @Override
    public Integer getContract(CustomerRuleEntity customerRule) {
        JSONObject retObject = (JSONObject) toObject(customerRule);
        if (retObject == null) {
            return null;
        }
        return retObject.getInteger("contract");
    }

    @Override
    public Integer getOpportunity(CustomerRuleEntity customerRule) {
        JSONObject retObject = (JSONObject) toObject(customerRule);
        if (retObject == null) {
            return null;
        }
        return retObject.getInteger("opportunity");
    }

    @Override
    public JSONArray getCustomerStatus(CustomerRuleEntity customerRule) {
        JSONObject retObject = (JSONObject) toObject(customerRule);
        if (retObject == null) {
            return null;
        }
        return retObject.getJSONArray("customerStatus");
    }

    @Override
    public Integer getType(CustomerRuleEntity customerRule) {
        JSONObject retObject = (JSONObject) toObject(customerRule);
        if (retObject == null) {
            return null;
        }
        return retObject.getInteger("type");
    }

    @Override
    public JSONArray getRestrict(CustomerRuleEntity customerRule) {
        JSONObject retObject = (JSONObject) toObject(customerRule);
        if (retObject == null) {
            return null;
        }
        return retObject.getJSONArray("restrict");
    }

}
